package com.zhangxu.microservice.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class LoggingGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        log.info("🚀 Request: {} {}", request.getMethod(), request.getPath());
        return chain.filter(exchange)
                .doOnSuccess(aVoid -> log.info("✅ Response: {}", exchange.getResponse().getStatusCode()))
                .doOnError(ex -> log.error("💥 Response: {}", ex.getMessage()));
    }

    @Override
    public int getOrder() {
        return -100; // 比较靠前执行
    }
}
